package com.trf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.trf.entity.Product;
import com.trf.entity.R;
import com.trf.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 商品Controller
 *
 * @author 小小唐
 * @Date 2022/5/31-星期二-20:20
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService iProductService;

    /**
     * 查询轮播商品
     *
     * @return
     */
    @GetMapping("/findSwiper")
    public R findSwiper(){
        List<Product> swiperProductList = iProductService.list(new QueryWrapper<Product>().eq("isSwiper", true).orderByAsc("swiperSort"));
        Map<String,Object> map = new HashMap<>();
        map.put("message",swiperProductList);
        return R.ok(map);
    }

    /**
     * 查询热门推荐商品8个
     *
     * 注意分页操作，需要在mybatis里面设置
     * @return
     */
    @GetMapping("/findHot")
    public R findHot(){
        //分页
        Page<Product> page = new Page<>(0, 8);
        Page<Product> pageProduct = iProductService.page(page, new QueryWrapper<Product>().eq("isHot", true).orderByAsc("hotDateTime"));
        List<Product> hotProductList = pageProduct.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("message",hotProductList);
        return R.ok(map);

    }
}
